#https://blog.csdn.net/zong596568821xp/article/details/89644654
# pip  install --upgrade onvif_zeep

from time import sleep
 
from onvif import ONVIFCamera
import zeep
 
XMAX = 1
XMIN = -1
YMAX = 1
YMIN = -1
 
def zeep_pythonvalue(self, xmlvalue):
    return xmlvalue
 
def perform_move(ptz, request, timeout):
    # Start continuous move
    ptz.ContinuousMove(request)
    # Wait a certain time
    sleep(timeout)
    # Stop continuous move
    ptz.Stop({'ProfileToken': request.ProfileToken})
 
 
def absolute_move(pan,pan_speed,tilt,tilt_speed,zoom,zoom_speed):

    #mycam = ONVIFCamera('192.168.1.67', 80, 'admin', 'hik123456')
    #mycam = ONVIFCamera('192.168.1.106', 80, 'admin', 'admin123456')
    mycam = ONVIFCamera('192.168.1.22', 80, 'admin', 'hw123456')
    # Create media service object
    media = mycam.create_media_service()
    # Create ptz service object
    ptz = mycam.create_ptz_service()
 
    # Get target profile
    zeep.xsd.simple.AnySimpleType.pythonvalue = zeep_pythonvalue
    media_profile = media.GetProfiles()[0]
 
    # Get PTZ configuration options for getting absolute move range
    request = ptz.create_type('GetConfigurationOptions')
    request.ConfigurationToken = media_profile.PTZConfiguration.token
    # ptz_configuration_options = ptz.GetConfigurationOptions(request)
 
    request = ptz.create_type('AbsoluteMove')
    request.ProfileToken = media_profile.token
    ptz.Stop({'ProfileToken': media_profile.token})
 
    if request.Position is None:
        request.Position = ptz.GetStatus({'ProfileToken': media_profile.token}).Position
    if request.Speed is None:
        request.Speed = ptz.GetStatus({'ProfileToken': media_profile.token}).Position
 
    request.Position.PanTilt.x = pan
    request.Speed.PanTilt.x = pan_speed
 
    request.Position.PanTilt.y = tilt
    request.Speed.PanTilt.y = tilt_speed
 
    request.Position.Zoom = zoom
    request.Speed.Zoom = zoom_speed
 
    ptz.AbsoluteMove(request)
    sleep(5)
    print('finish p:{0},t:{1},z:{2}'.format(pan,tilt,zoom))
 
 
if __name__ == '__main__':
    for i in range(30):
        for j in range(30):
            absolute_move(j/30,1,i/30,1,1,1)
    #  absolute_move(-1,1,0,1,1,1)
    #  sleep(10)
    #  absolute_move(0,1,0,1,1,1)
    #  sleep(10)
    #  absolute_move(1,1,0,1,1,1)
    #  sleep(10)